<?php

namespace app\api\logic\user;

use think\facade\Request;
use app\common\repository\user\R as UserRRepository;
use app\common\repository\authView\R as AuthViewRRepository;

class UserAuth
{
    public function __construct(
        UserRRepository $userRRepository,
        AuthViewRRepository $authViewRRepository
    ) {
        $this->userRRepository = $userRRepository;
        $this->authViewRRepository = $authViewRRepository;
    }

    public function check()
    {
        $path = Request::param('path');
        if (empty($path)) {
            return ['done' => false, 'code' => config('code.common.paramsRequire')];
        }
        $limitView = $this->authViewRRepository->getAuthView(['path' => $path]);
        if (empty($limitView->id)) {
            return [];
        }
        $userAuthView = $this->userRRepository->getUserAuthView(request()->user->id);
        if (in_array($path, $userAuthView)) {
            return [];
        }
        return ['data' => '未拥有权限', 'code' => config('code.user.auth.limit')];
    }
}
